package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
//        System.out.println("删除图书");
        System.out.println("请输入删除图书的名字");
        String name = scanner.nextLine();
        int count = bookList.getCount();
        int index = 0;
        int i = 0;
        for (i = 0; i < count; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName())){
//                找到删除的点
                index = i;
                break;
            }
        }
        if (i>=count){
            System.out.println("没有要删除的书");
            return;
        }
//        进行删除
        for (int j = index; j < count-1; j++) {
            Book book =bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
//        最后图书位置置为空
        bookList.setBook(count,null);

        bookList.setCount(count-1);
        System.out.println("删除成功");
    }
}
